﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class mycontrols_upfile : System.Web.UI.UserControl
{
    private string _filepath;
    public string Filepath
    {
        get { return _filepath; }
        set { _filepath = value; }
    }
    public string getpath()
    {
        this.Filepath = this.fileimages.Text;
        return Filepath;
    }
    public void setPic()
    {
        this.fileimages.Text = "";
    
    }
    public void setPath(string path)
    {
        this.fileimages.Text = path;
    
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string extend = this.FileUpload1.FileName;//获取扩展名
        extend = extend.Substring(extend.LastIndexOf(".")+1);
            string name = DateTime.Now.ToString("yyyyMMddHHmmssffff")+"."+extend;//文件名字
            string size = this.FileUpload1.PostedFile.ContentLength.ToString();//文件大小
            string type = this.FileUpload1.PostedFile.ContentType;//文件类型ype == "image/pjpeg" || type == "image/gif" || type == "x-png"
            //string type2 = name.Substring(name.LastIndexOf(".") + 1);//文件类型
            string path = Server.MapPath("~/pic/") + "//" + name;//实际路径
            string datapath = "pic/" +name;
            if (Convert.ToInt32(size) > 2048*1000)
            {
                this.lblMsg.Text = "上传失败文件大于2m";

            }

            else if (type.Equals("image/gif") || type.Equals("image/bmp") || type.Equals("image/pjpeg") || type.Equals("image/x-png") || type.Equals("image/jpg"))
            {

                this.FileUpload1.SaveAs(path);
                this.lblMsg.Text = "上传成功";
                this.fileimages.Text = datapath;
            }
            else
            {

                this.lblMsg.Text = "文件类型不对上传失败";
            }
       
    }
}
